<template>
  <c-select
    v-bind="$attrs"
    v-on="$listeners"
    :loading="loading"
    filterable
    :value="value"
    :options="banks"
    @change="hangleChange"
    :props="props"
  ></c-select>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    busType: String,
    otherParams: {
      type: Object,
      default: () => ({}),
    },
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data() {
    return {
      props: {
        value: 'agency_id',
        label: 'agency_name',
      },
      banks: [],
      loading: false,
    }
  },
  computed: {
    ...mapGetters(['user', 'org']),
    address() {
      return this.user.queryAddressCode
    },
    bankType() {
      return this.user.clientOrgClassType
    },
    isSYSTEM() {
      // 系统后台查所有
      return this.org.orgClass === 'org_credit_sys'
    },
  },
  watch: {
    otherParams: {
      handler: function () {
        this.getData()
      },
      deep: true,
    },
  },
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      const params = this.isSYSTEM
        ? { ...this.otherParams, 'agency_type.eq': 1 }
        : {
            'agency_type.eq': 1,
            'agency_type_sub.eq': this.bankType,
            address_code: this.address,
          }
      this.$api
        .getData(this.$serviceName, 'credit_agency_info_view', params, { org_region_lever: 'asc' })
        .then(res => {
          this.banks = res?.data ?? []
        })
        .finally(() => {
          this.loading = false
        })
    },
    hangleChange(val) {
      const select = this.banks.find(item => item.agency_id === val) || {}
      this.$emit('select', select)
    },
  },
}
</script>

<style lang="scss" scoped></style>
